package cn.icanci.ladybird.interceptor;

import cn.icanci.ladybird.common.util.security.JWTUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: icanci
 * @date: Created in 2020/10/9 12:24
 * @classAction: 用户的登录注册校验
 */
@SuppressWarnings("all")
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            String accessToken = request.getHeader("token");
            if (null == accessToken) {
                accessToken = request.getParameter("token");
            }
            if (null == accessToken) {
                Cookie[] cookies = request.getCookies();
                for (Cookie cookie : cookies) {
                    System.out.println(cookie.getName());
                    System.out.println(cookie.getValue());
                    if ("token".equals(cookie.getName())) {
                        accessToken = cookie.getValue();
                        break;
                    }
                }
            }
            if (null == accessToken) {
                accessToken = String.valueOf(request.getSession().getAttribute("token"));
            }
            log.info("UserLoginInterceptor.preHandle 最终的Token " + accessToken);
            if (null != accessToken) {
                log.info(accessToken);
                Claims claims = JWTUtils.checkJWT(accessToken);
                if (null == claims) {
                    // 跳转到登录界面
                    response.sendRedirect("/api/v1/pri/user/do/login-by-account");
                    return false;
                }
                String userHeadImg = (String) claims.get("user_head_img");
                Long userId = Long.valueOf(claims.get("user_id").toString());
                String username = (String) claims.get("username");
                Integer userEnabled = (Integer) claims.get("user_enabled");
                // 判断用户的权限
                // 等于0就是没有权限
                if (userEnabled.equals(0)) {
                    response.sendRedirect("/api/v1/pub/user/no-authority");
                    return false;
                }
                // 拥有权限
                request.setAttribute("user_head_img", userHeadImg);
                request.setAttribute("user_id", userId);
                request.setAttribute("username", username);
                request.setAttribute("user_enabled", userEnabled);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("/api/v1/pri/user/do/login-by-account");
            return false;
        }
        response.sendRedirect("/api/v1/pri/user/do/login-by-account");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
